
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Filib
 */
public class ComunicacionHttp {

    private static HttpConnection conexion;
    private static OutputStream out;
    private static InputStream input;
    private static ByteArrayOutputStream bout = new ByteArrayOutputStream(); ;
    private static byte[] buffer;
    private static int bytesread;


    public ComunicacionHttp()
    {

    }

    public static synchronized String enviar(String url, String parameters) throws Exception
    {
        String response = null;

        try
        {
           //Http Request
           conexion = (HttpConnection) Connector.open(url,Connector.READ_WRITE);
           conexion.setRequestMethod(HttpConnection.POST);
           conexion.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            out = conexion.openOutputStream();
            out.write(parameters.getBytes());

            if(conexion.getResponseCode()==HttpConnection.HTTP_OK)
            {
                input = conexion.openInputStream();
                bout = new ByteArrayOutputStream();
                buffer = new byte[512];

                while ((bytesread = input.read(buffer))>0)
                {
                    bout.write(buffer,0,bytesread);
                }

                response = new String(bout.toByteArray());
                bout.close();
                input.close();

                if(conexion!=null)
                {
                    conexion.close();
                }
            }
            else
            {
                throw new Exception("¡No Server!");
            }

        }
        catch(IOException ioe)
        {
            response = null;
            throw new Exception("Problema de I/O: " + ioe.getMessage());
        }
        catch(Exception ex)
        {
            response = null;
            throw new Exception("WEB: " + ex.getMessage());
        }
        finally
        {
            if(out!=null)
            {
                try
                {
                    out.close();
                    out = null;
                }
                catch(Exception ex)
                {
                }
            }

            if(bout != null)
            {
                try
                {bout.close();
                 bout = null;
                }
                catch(Exception ex)
                {}
            }
            //VAS por Input
            if(input!=null)
            {
                try
                {
                    input.close();
                    input=null;
                }
                catch(Exception ex)
                {}
            }

            if(conexion!=null)
            {
                try
                {
                    conexion.close();
                    conexion = null;
                }
                catch(Exception ex)
                {}
            }
            System.gc();
        }
        return response;
    }

    public static String urlEncode(String url)
    {
        String enc = "";

        for(int i=0; i<url.length();i++)
            {
                enc += (url.charAt(i) == ' ') ? "%20" : (url.charAt(i) == '#') ? "%23" :
                (url.charAt(i) == 'Ê') ? "%CA" : (url.charAt(i) == 'Æ') ? "%C6" :
                    String.valueOf(url.charAt(i));
            }
        return enc;
    }

}
